# ################################################################# #
#### LOAD LIBRARY AND DEFINE CORE SETTINGS                       ####
# ################################################################# #

### Clear memory
rm(list = ls())

### Load Apollo library
library(apollo)

### Set working directory to the folder of this script
apollo_setWorkDir()

### Initialise code
apollo_initialise()

### Set core controls
apollo_control = list(
  modelDescr      = "HB model on mode choice SP data, mix of random and non-random parameters",
  indivID         = "ID",
  HB              = TRUE, 
  outputDirectory = "output"
)

# ################################################################# #
#### LOAD DATA AND APPLY ANY TRANSFORMATIONS                     ####
# ################################################################# #

### Loading data from package
### if data is to be loaded from a file (e.g. called data.csv), 
### the code would be: database = read.csv("data.csv",header=TRUE)
database = apollo_modeChoiceData
### for data dictionary, use ?apollo_modeChoiceData

### Use only SP data
database = subset(database,database$SP==1)

### Create new variable with average income
database$mean_income = mean(database$income)

# ################################################################# #
#### DEFINE MODEL PARAMETERS                                     ####
# ################################################################# #

### Vector of parameters, including any that are kept fixed in estimation
apollo_beta=c(asc_car                     = 0,
              asc_bus                     = 0,
              asc_air                     = 0,
              asc_rail                    = 0,
              asc_bus_interaction_female  = 0,
              asc_air_interaction_female  = 0,
              asc_rail_interaction_female = 0,
              b_tt_car                    = -3,
              b_tt_bus                    = -3,
              b_tt_air                    = -3,
              b_tt_rail                   = -3,
              b_tt_interaction_business   = 0,
              b_access                    = -3,
              b_cost                      = -3,
              b_cost_interaction_business = 0,
              cost_income_elast           = 0,
              b_no_frills                 = 0,
              b_wifi                      = 0,
              b_food                      = 0)

### Vector with names (in quotes) of parameters to be kept fixed at their starting value in apollo_beta, use apollo_fixed = c() if none
apollo_fixed = c("asc_car","b_no_frills")

# ################################################################# #
#### HB settings                                                 ####
# ################################################################# #

apollo_HB = list(
  hbDist         = c(asc_car                     = "NR",
                     asc_bus                     = "N",
                     asc_air                     = "N",
                     asc_rail                    = "N",
                     asc_bus_interaction_female  = "NR",
                     asc_air_interaction_female  = "NR",
                     asc_rail_interaction_female = "NR",
                     b_tt_car                    = "LN-",
                     b_tt_bus                    = "LN-",
                     b_tt_air                    = "LN-",
                     b_tt_rail                   = "LN-",
                     b_tt_interaction_business   = "NR",
                     b_access                    = "LN-",
                     b_cost                      = "LN-",
                     b_cost_interaction_business = "NR",
                     cost_income_elast           = "NR",
                     b_no_frills                 = "NR",
                     b_wifi                      = "CN+",
                     b_food                      = "CN+"),
  gSeed           = 13,     # seed for the random number generator
  gNCREP          = 200000, # burn-in iterations
  gNEREP          = 20000,  # post burn-in iterations
  gINFOSKIP       = 500)

# ################################################################# #
#### GROUP AND VALIDATE INPUTS                                   ####
# ################################################################# #

apollo_inputs = apollo_validateInputs()

# ################################################################# #
#### DEFINE MODEL AND LIKELIHOOD FUNCTION                        ####
# ################################################################# #

apollo_probabilities=function(apollo_beta, apollo_inputs, functionality="estimate"){

  ### Attach inputs and detach after function exit
  apollo_attach(apollo_beta, apollo_inputs)
  on.exit(apollo_detach(apollo_beta, apollo_inputs))

  ### Create list of probabilities P
  P = list()
  
  ### Create alternative specific constants and coefficients using interactions with socio-demographics
  asc_bus_value   = asc_bus  + asc_bus_interaction_female * female
  asc_air_value   = asc_air  + asc_air_interaction_female * female
  asc_rail_value  = asc_rail + asc_rail_interaction_female * female
  b_tt_car_value  = b_tt_car + b_tt_interaction_business * business
  b_tt_bus_value  = b_tt_bus + b_tt_interaction_business * business
  b_tt_air_value  = b_tt_air + b_tt_interaction_business * business
  b_tt_rail_value = b_tt_rail + b_tt_interaction_business * business
  b_cost_value    = ( b_cost +  b_cost_interaction_business * business ) * ( income / mean_income ) ^ cost_income_elast
  
  ### List of utilities: these must use the same names as in mnl_settings, order is irrelevant  V = list()
  V=list()
  V[["car"]]  = asc_car        + b_tt_car_value  * time_car                           + b_cost_value * cost_car
  V[["bus"]]  = asc_bus_value  + b_tt_bus_value  * time_bus  + b_access * access_bus  + b_cost_value * cost_bus 
  V[["air"]]  = asc_air_value  + b_tt_air_value  * time_air  + b_access * access_air  + b_cost_value * cost_air   + b_no_frills * ( service_air == 1 )  + b_wifi * ( service_air == 2 )  + b_food * ( service_air == 3 )
  V[["rail"]] = asc_rail_value + b_tt_rail_value * time_rail + b_access * access_rail + b_cost_value * cost_rail  + b_no_frills * ( service_rail == 1 ) + b_wifi * ( service_rail == 2 ) + b_food * ( service_rail == 3 )
  
  ### Define settings for MNL model component
  mnl_settings = list(
    alternatives = c(car=1, bus=2, air=3, rail=4), 
    avail        = list(car=av_car, bus=av_bus, air=av_air, rail=av_rail), 
    choiceVar    = choice, 
    utilities    = V
  )
  
  ### Compute probabilities using MNL model
  P[["model"]] = apollo_mnl(mnl_settings, functionality)

  ### Prepare and return outputs of function
  P = apollo_prepareProb(P, apollo_inputs, functionality)
  return(P)
}

# ################################################################# #
#### MODEL ESTIMATION                                            ####
# ################################################################# #

model = apollo_estimate(apollo_beta, apollo_fixed, apollo_probabilities, apollo_inputs)

# ################################################################# #
#### MODEL OUTPUTS                                               ####
# ################################################################# #

# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO SCREEN)                               ----
# ----------------------------------------------------------------- #

apollo_modelOutput(model)

# ----------------------------------------------------------------- #
#---- FORMATTED OUTPUT (TO FILE, using model name)               ----
# ----------------------------------------------------------------- #

apollo_saveOutput(model)

# ################################################################# #
##### POST-PROCESSING                                            ####
# ################################################################# #

### Print outputs of additional diagnostics to new output file (remember to close file writing when complete)
apollo_sink()

# ----------------------------------------------------------------- #
#---- MODEL PREDICTIONS AND ELASTICITY CALCULATIONS              ----
# ----------------------------------------------------------------- #

### Use the estimated model to make predictions
predictions_base = apollo_prediction(model, apollo_probabilities, apollo_inputs)

### Look at summary of the predicted choice probabilities
summary(predictions_base[,3:6])

### Now imagine the cost for rail increases by 1%
database$cost_rail = 1.01*database$cost_rail

### Rerun predictions with the new data, and save into a separate matrix
apollo_inputs = apollo_validateInputs()
predictions_new = apollo_prediction(model, apollo_probabilities, apollo_inputs)

### Look at summary of the predicted choice probabilities
summary(predictions_new[,3:6])

### Return to original data
database$cost_rail = 1/1.01*database$cost_rail
apollo_inputs = apollo_validateInputs()

### Compute change in probabilities
change=(predictions_new-predictions_base)/predictions_base

### Focus only on predictions for the four alternatives
change=change[,3:6]

### Look at first individual
change[database$ID==1,]
### And person 9, who has all 4 modes available
change[database$ID==9,]

### Summary of changes (possible presence of NAs for unavailable alternatives)
summary(change)

### Look at mean changes for subsets of the data, ignoring NAs
colMeans(change,na.rm=TRUE)
colMeans(subset(change,database$business==1),na.rm=TRUE)
colMeans(subset(change,database$business==0),na.rm=TRUE)
colMeans(subset(change,(database$income<quantile(database$income,0.25))),na.rm=TRUE)
colMeans(subset(change,(database$income>=quantile(database$income,0.25))|(database$income<=quantile(database$income,0.75))),na.rm=TRUE)
colMeans(subset(change,(database$income>quantile(database$income,0.75))),na.rm=TRUE)

### Compute own elasticity for rail:
log(sum(predictions_new[,6])/sum(predictions_base[,6]))/log(1.01)

### Compute cross-elasticities for other modes
log(sum(predictions_new[,3])/sum(predictions_base[,3]))/log(1.01)
log(sum(predictions_new[,4])/sum(predictions_base[,4]))/log(1.01)
log(sum(predictions_new[,5])/sum(predictions_base[,5]))/log(1.01)

# ----------------------------------------------------------------- #
#---- switch off writing to file                                 ----
# ----------------------------------------------------------------- #

apollo_sink()

